var username;
var password;

//Wait for PhoneGap to connect with the device
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}


// Transaction error callback
function error(err) {
    alert("Error processing SQL: "+err);
}

// Transaction success callback
function success() {
    alert("Login credentials saved!");
}

function displayCode(){
	//document.getElementById('result').innerHTML=document.getElementById('code').value;
	newIframe=document.getElementById('result');
	newIframe.contentWindow.document.open('text/html', 'replace');
	newIframe.contentWindow.document.write(document.getElementById('code').value);
	newIframe.contentWindow.document.close();
	$('#displayer').trigger('expand');
	$('#header').trigger('collapse');
}


function registerUser() {
	var ok=true;
	name=document.getElementById('register_name').value;
	password=document.getElementById('register_pass').value;
	email=document.getElementById('register_email').value;
	
	if (name==""){
		alert("Please fill in your name!");
		ok=false;
	}
	if (email==""){
		alert("Please fill in your email address!");
		ok=false;
	}
	if (password==""){
		alert("Please fill in your password!");
		ok=false;
	}
	if (ok){
    	Android.registerUser(name,password,email);
	}
	return ok;
}

function loginUser() {
	var ok=true;
	password=document.getElementById('login_pass').value;
	email=document.getElementById('login_email').value;
	
	if (email==""){
		alert("Please fill in your email address!");
		ok=false;
	}
	if (password==""){
		alert("Please fill in your password!");
		ok=false;
	}
	if (ok){
    	Android.loginUser(password,email);
	}
	return ok;
}

function loadCategories() {
	var html_text = Android.getCategories();
	document.body.innerHTML = document.body.innerHTML.replace("%CHAPTERS%", html_text);
	$('#all_content').trigger('create');
}

function loadContent() {
	var html_text = Android.getContent();
	document.body.innerHTML = document.body.innerHTML.replace("%CONTENT%", html_text);
	$('#all_content').trigger('create');
}

function loadQuizes() {
	var html_text = Android.getQuizes();
	document.body.innerHTML = document.body.innerHTML.replace("%QUIZES%", html_text);
	$('#all_content').trigger('create');
}

function loadQuiz() {
	var html_text = Android.getQuestions();
	document.body.innerHTML = document.body.innerHTML.replace("%QUESTIONS%", html_text);
	$('#all_content').trigger('create');
}
function loadTest() {
	var html_text = Android.getPracticalTest();
	document.body.innerHTML = document.body.innerHTML.replace("%EXERCISE%", html_text);
	$('#all_content').trigger('create');
}

function validate(){
	var i = 0;
	var options = new Array();
    $("input[name*=radio-choice-]:checked").each(function() {
    	options[i] = $(this).val();
		i++;
    });
	Android.validate(options);
	window.location.replace("file:///android_asset/www/quizValidationScreen.html");
}

function loadQuizValidation() {
	var html_text = Android.loadValidation();
	document.body.innerHTML = document.body.innerHTML.replace("%QUIZ%", html_text);
	$('#all_content').trigger('create');
	$("input[type='radio']").each(function() {
    	$("input[type='radio']").checkboxradio('disable');
    });
}

function quizListRedirect(){
	Android.quizListRedirect();
}

function viewLessonContent(lesson_id){
	Android.redirect(lesson_id);
}

function exercisesRedirect(){
	Android.exercisesRedirect();
}

function quizRedirect1(lesson_id){
	Android.quizRedirect(lesson_id);
}

function quizRedirect(){
	Android.quizRedirect();
}
function switchToRegister(){
	Android.switchToRegister();
}
function switchToLessons(){
	Android.switchToLessons();
}
function switchToTakeQuiz(){
	Android.switchToTakeQuiz();
}
function switchToPracticalTest(){
	Android.switchToPracticalTest();
}
function switchToDoItYourself(){
	Android.switchToDoItYourself();
}
function switchToRegister(){
	Android.switchToRegister();
}